<?php
class Database {
    private $connection;
    private $config;
    private $error;
    
    public function __construct() {
        $this->config = new DatabaseConfig();
        $this->connect();
    }
    
    private function connect() {
        $dsn = "mysql:host=" . $this->config->host . ";dbname=" . $this->config->dbname . ";charset=" . $this->config->charset;
        
        try {
            $this->connection = new PDO($dsn, $this->config->username, $this->config->password);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        } catch(PDOException $e) {
            die("Connection failed: " . $e->getMessage());
        }
    }
    
    public function query($sql, $params = array()) {
        try {
            $stmt = $this->connection->prepare($sql);
            $stmt->execute($params);
            return $stmt;
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
            error_log("Database Error: " . $this->error);
            return false;
        }
    }
    
    public function getError() {
        return $this->error;
    }
}


